<?php
namespace Admin\Model;

use Think\Model;

class UserModel extends Model{

    const STATUS = 1;
    /**
     * 公共构造方法
     */
    public function __construct()
    {
        parent::__construct();
        $this->user = M("user");
        $this->Model = new Model();
        $this->connection = $this->db(1,"mysql://root:123456@localhost:3306/mmy2");
    }


    /**
     *  function select
     * @return mixed
     */
    public function getUserList($page,$limit)
    {
        $page = $page-1;
        $offset = $page*$limit;
        $data =  $this->user->where('status = 1 and type = 1')->limit($offset,$limit)->select();
        return $data;
    }

    /**
     * 获取管理员列表
     * @author mmy
     * @return mixed
     */
    public function getAdminTotal()
    {
        $data =  $this->user->where('status = 1 and type = 1')->count("*" );
        return $data;
    }

    /**
     *  function select
     * @return mixed
     */
    public function getUserExist($username,$password)
    {
        $where = "status=".self::STATUS." and name ='".$username."' and pass='".md5($password)."'";
        $data =  $this->user->where($where)->select();
        return $data;
    }

    /**
     * function insert
     * @param $data
     * @return bool
     */
    public function InsertUser($data)
    {
        if(!count($data))
        {
            return false;
        }

        $res = $this->user->data($data)->add();
        return $res;

    }

    /**
     * function mode
     * @param $data
     * @return bool
     */
    public function ModUser($data)
    {
        if(!count($data))
        {
            return false;
        }

        $res = $this->user->save($data);
        return $res;
    }

    /**
     * function delete
     * @param $UserId
     * @return bool
     */
    public function DeleteUser($UserId)
    {
        if(empty($UserId))
        {
            return false;
        }

        $res = $this->user->where('id='.$UserId)->delete();
        return $res;

    }

    /**
     * 切换数据库获取数据
     */
    public function getUserList2()
    {
        $sql = "select id,name,status from mmy2_person";
        $res =$this->connection->query($sql);
        return $res;
    }

}


